/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package darknet.core.impl.net;

import darknet.core.spec.net.Protocol;

/**
 *
 * @author Yoyo
 */
public class BaseProtocol implements Protocol {

    /** Instance of the last version of BaseProtocol. */
    private static BaseProtocol CURRENT;

    /** Serial version */
    private static final long serialVersionUID = 1L;

    /**
     * Get the current version of BaseProtocol.
     * @return
     */
    public synchronized static BaseProtocol getCurrent() {

	if(CURRENT == null) {

	    String name = "Base Protocol";
	    String version = "0.1";
	    CURRENT =  new BaseProtocol(name, version);
	}
	return CURRENT;
    }

    /** Protocol's name */
    private String name;

    /** Protocol's version */
    private String version;

    /**
     * Initialise a new BaseProtocol.
     *
     * @param name
     * @param version
     */
    private BaseProtocol(String name, String version) {

	if (name == null || name.length() == 0) {
	    throw new IllegalArgumentException("name is invalid");
	}
	this.name = name;

	if (version == null || version.length() == 0) {
	    throw new IllegalArgumentException("version is invalid");
	}
	this.version = version;
    }

    @Override
    public String getName() {
	return name;
    }

    @Override
    public String getVersion() {
	return version;
    }

    @Override
    public boolean equals(Object obj) {
	return obj instanceof BaseProtocol &&
		((BaseProtocol)obj).name.equals(name) &&
		((BaseProtocol)obj).version.equals(version);
    }

    @Override
    public int hashCode() {
	return toString().hashCode();
    }

    @Override
    public String toString() {
	return new StringBuilder().append("name: ").append(name)
	.append("; version:").append(version).toString();
    }
}
